package cn.doctorpda.conference.ui.personal.meeting;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import cn.doctorpda.conference.R;
import cn.doctorpda.conference.ui.BaseFragment;

/**
 * Created by huangjinlong on 2016/5/17.
 */
public class MyMeetingFragment extends BaseFragment implements View.OnClickListener {

    private String[] titles = new String[]{"待参加", "待处理", "已结束/取消"};
    private TabLayout tab_layout;
    private ViewPager info_viewpager;
    private List<Fragment> fragments = new ArrayList<>();
    private FragmentPagerAdapter mPagerAdater;

    public static MyMeetingFragment newInstance() {
        MyMeetingFragment fragment = new MyMeetingFragment();
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_my_meeting, container, false);
        initView(view);
        initData();
        return view;
    }

    private void initData() {
        fragments.add(MyMeetingFragmentTab.newInstance("future"));
        fragments.add(MyMeetingFragmentTab.newInstance("now"));
        fragments.add(MyMeetingFragmentTab.newInstance("old"));

        mPagerAdater = new FragmentPagerAdapter(getActivity().getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return titles[position];
            }
        };
        info_viewpager.setAdapter(mPagerAdater);
        info_viewpager.setCurrentItem(0);
        info_viewpager.setOffscreenPageLimit(2);
        tab_layout.setupWithViewPager(info_viewpager);
        tab_layout.setTabMode(TabLayout.MODE_FIXED);

    }

    private void initView(View view) {

        view.findViewById(R.id.btn_back).setOnClickListener(this);
        tab_layout = (TabLayout) view.findViewById(R.id.tab_layout);
        info_viewpager = (ViewPager) view.findViewById(R.id.info_viewpager);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.btn_back:
                getActivity().finish();
                break;
        }

    }

    @Override
    public String getPageName() {
        return "MyMeetingFragment";
    }
}
